package com.uubx.service.impl;

import com.uubx.dao.InsuranceDescDao;
import com.uubx.exception.CustomerException;
import com.uubx.mapper.InsuranceMapper;

import com.uubx.pojo.Insurance;
import com.uubx.pojo.InsuranceDesc;
import com.uubx.result.MessageCode;
import com.uubx.service.InsuranceService;
import com.uubx.vo.InsuranceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.GeneratedValue;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author wyy
 * @Date 2020/12/30 19:48
 * @Version 1.0
 */
@Service
public class InsuranceServiceImpl implements InsuranceService {

    @Autowired
    private InsuranceMapper insuranceMapper;

    @Autowired
    private InsuranceDescDao insuranceDescDao;



    @Override
    public void insertInsurance(Insurance insurance,String desc) {

        //插入保险的基本信息
        //设置保险的状态为下架
        insurance.setStatus(2);

        //设置创建时间和修改时间
        insurance.setCreated(new Date());
        insurance.setUpdated(insurance.getCreated());
        insuranceMapper.insert(insurance);

        //插入商品的详情
        InsuranceDesc insuranceDesc=new InsuranceDesc();
        insuranceDesc.setId(insurance.getId()+"");
        //插入商品描述
        insuranceDesc.setInsuranceDesc(desc);
        insuranceDesc.setCreated(insurance.getCreated());
        insuranceDesc.setUpdated(insurance.getCreated());

        //调用dao插入
        insuranceDescDao.addInsuranceDesc(insuranceDesc);

    }

    @Override
    public void updateInsurance(Insurance insurance,String desc) {

        insurance.setUpdated(new Date());
        insuranceMapper.updateByPrimaryKeySelective(insurance);

        //修改保险描述的详情信息
        InsuranceDesc insuranceDesc=new InsuranceDesc();
        insuranceDesc.setId(insurance.getId()+"");
        //插入商品描述
        insuranceDesc.setInsuranceDesc(desc);
        insuranceDesc.setCreated(insurance.getCreated());
        insuranceDesc.setUpdated(insurance.getCreated());

        //调用dao插入
        insuranceDescDao.updateInsuranceDesc(insuranceDesc);
    }

    @Override
    public Insurance findInsuranceById(Long id) {

        return insuranceMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateInsuranceStatus(Insurance insurance) {
        //修改商品的状态 1为正常 2为下架 3为删除   假删除  逻辑删除
        insuranceMapper.updateByPrimaryKeySelective(insurance);
    }

    @Override
    public List<Map> findInsuranceWhere(InsuranceVo insuranceVo) throws CustomerException {



        return insuranceMapper.findInsuranceWhere(insuranceVo);
    }

    @Override
    public String findInsuranceDescById(Long id) throws CustomerException {
        //根据id获取到保单详情的id
        InsuranceDesc insuranceDescById = insuranceDescDao.findInsuranceDescById(id.toString());
        //判空
        if(insuranceDescById==null){
            throw new CustomerException(MessageCode.ERROR);
        }
       //把这个id的详情获取返回
        return insuranceDescById.getInsuranceDesc();
    }
}
